package encore.portal.config.impl;

import java.io.InputStream;
import java.util.List;
import java.util.Set;

import javax.naming.ConfigurationException;
import javax.servlet.ServletContext;

import encore.portal.PortalConfigurationException;
import encore.portal.config.RenderConfigAdminService;
import encore.portal.config.RenderConfigService;
import encore.portal.config.ResourceConfig;
import encore.portal.config.ResourceConfigReader;
import encore.portal.models.PageConfig;

public class XMLRenderConfigService implements RenderConfigAdminService,
		RenderConfigService {

	private ResourceConfig config;

	public XMLRenderConfigService()
	{
	 config=null;	
	}
	
	
	public String getPortalName() {
		return config.getPortalName();
	}

	public String getPortalVersion() {
		return config.getPortalVersion();
	}

	public String getContainerName() {
		return config.getContainerName();
	}

	public Set getSupportedPortletModes() {
		return config.getSupportedPortletModes();
	}

	public Set getSupportedWindowStates() {
		return config.getSupportedWindowStates();
	}

	@Override
	public void addPage(PageConfig pageConfig) throws ConfigurationException {

		config.getRenderConfig().addPage(pageConfig);
	}

	@Override
	public PageConfig getDefaultPage() {
		// TODO Auto-generated method stub
		return config.getRenderConfig().getPageConfig(null);
	}

	@Override
	public PageConfig getPage(String pageId) {
		// TODO Auto-generated method stub
		return config.getRenderConfig().getPageConfig(pageId);
	}

	@Override
	public List getPages() {

		return config.getRenderConfig().getPages();
	}

	@Override
	public void removePage(PageConfig pageConfig) {
		this.config.getRenderConfig().removePage(pageConfig);
	}

	@Override
	public void destroy() throws PortalConfigurationException {
		// TODO Auto-generated method stub
		config = null;
	}

	@Override
	public void init(ServletContext servletContext)
			throws PortalConfigurationException {
		try {
			InputStream inputStream=servletContext.getResourceAsStream(ResourceConfigReader.CONFIG_FILE);
			config=ResourceConfigReader.getFactory().parse(inputStream);
		} catch (Exception e) {
			throw new PortalConfigurationException(e);
		}
	}

}
